package com.lebron.kotlin.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.ActivityOptionsCompat
import android.support.v7.widget.LinearLayoutManager
import com.lebron.kotlin.R
import com.lebron.kotlin.bean.movie.Subject
import com.lebron.kotlin.bean.movie.TheaterBean
import com.lebron.kotlin.bean.movie.USTheaterBean
import com.lebron.kotlin.ui.activity.MovieDetailActivity
import com.lebron.kotlin.ui.adapter.MovieListAdapter
import com.lebron.kotlin.ui.present.impl.MovieListPresentImpl
import com.lebron.kotlin.ui.view.MovieListView
import com.lebron.kotlin.utils.ToastUtil
import kotlinx.android.synthetic.main.fragment_list.*

/**
 * @author lebron
 * 电影列表页
 */
class MovieListFragment : BaseFragment(), MovieListView {

    private var subjects: ArrayList<Subject> = ArrayList()

    override fun setComingSoon(theaterBean: TheaterBean) {
        total = theaterBean.total
        attachData(theaterBean.subjects)
    }

    override fun setTop250(theaterBean: TheaterBean) {
        total = theaterBean.total
        attachData(theaterBean.subjects)
    }

    override fun setUSBox(theaterBean: USTheaterBean) {
        subjects.clear()
        theaterBean.subjects.forEach { subjects ->
            this.subjects.add(subjects.subject)
        }

        attachData(subjects)
    }


    private var adapter: MovieListAdapter? = null
    private var start: Int = 0
    private var count: Int = 10
    private var total: Int = 0

    override fun setTheaterData(theaterBean: TheaterBean) {
        total = theaterBean.total
        attachData(theaterBean.subjects)
    }

    override fun onError(error: String) {
        ToastUtil.toast(error)

        swipeRefreshLayout.isRefreshing = false
        adapter?.loadMoreComplete()
    }

    private fun attachData(subjects: List<Subject>) {

        if (start == 0) {
            adapter?.setNewData(subjects)
            swipeRefreshLayout.isRefreshing = false
        } else {
            adapter?.addData(subjects)
            adapter?.loadMoreComplete()
        }
    }

    private var present: MovieListPresentImpl? = MovieListPresentImpl()

    private var title: String? = null

    fun newInstance(title: String): MovieListFragment {
        val bundle = Bundle()
        bundle.putString("title", title)
        val fragment = MovieListFragment()
        fragment.arguments = bundle
        return fragment
    }

    override fun initData() {
        title = arguments?.getString("title")

        requestData()

    }

    private fun requestData() {

        when (title) {
            resources.getStringArray(R.array.movie_tab_type)[0] -> {
                present?.getTheaterData(start, count)
            }
            resources.getStringArray(R.array.movie_tab_type)[1] -> {
                present?.getComingSoon(start, count)
            }
            resources.getStringArray(R.array.movie_tab_type)[2] -> {
                present?.getTop250(start, count)
            }
            resources.getStringArray(R.array.movie_tab_type)[3] -> {
                present?.getUSBox(start, count)
            }
        }
    }

    override fun initView() {
        present?.attachView(this)

        recycler_view.layoutManager = LinearLayoutManager(requireContext())
        adapter = MovieListAdapter(null)
        recycler_view.adapter = adapter

        swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimaryDark)
        //下拉刷新
        swipeRefreshLayout.setOnRefreshListener {
            if (!adapter?.isLoading!!) {
                start = 0
                count = 10
                requestData()
            }
        }
        //上拉加载
        adapter?.setOnLoadMoreListener({
            if (!swipeRefreshLayout.isRefreshing) {
                if (total >= adapter?.data?.size!!) {
                    start += 10
                    count += 10
                    requestData()
                } else {
                    adapter?.loadMoreEnd(false)
                }
            }
        }, recycler_view)
        //item的点击事件
        adapter?.setOnItemClickListener { adapter, view, position ->
            val subject: Subject = adapter.data[position] as Subject
            val intent = Intent(requireActivity(), MovieDetailActivity::class.java)
            intent.putExtra("id", subject.id)
            intent.putExtra("imgUrl",subject.images.large)
            intent.putExtra("title",subject.title)
            startActivity(intent, ActivityOptionsCompat.makeSceneTransitionAnimation(requireActivity(),view.findViewById(R.id.iv_movie_img),"movie_img").toBundle())
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_list
    }


    override fun onDestroy() {
        super.onDestroy()
        present?.detachView()
    }

}
